package yanyan.com.tochar;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.HttpUtil;
import yanyan.com.tochar.utils.StringUtil;
import yanyan.com.tochar.utils.ToastUtil;

/**
 * 域名查询
 */
public class FunsDomainNameSelectActivity extends Activity {

    private EditText domainNameEd;
    private TextView resEd;
    private ProgressDialog loadin;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funs_domain);
        domainNameEd=findViewById(R.id.domain_name);
        resEd=findViewById(R.id.domain_res);

        //统计各功能
        CoreUtil.statisticalFuns(this,"funs_domain_name");


    }

    /**
     * 域名查询
     * @param doName
     */
    private void domainName(String doName){
        loadin=new ProgressDialog(this);
        loadin.setMessage("正在查询...");
        loadin.show();
        Call msg = HttpUtil.getMsg("http://m.tool.chinaz.com/beian?s=" + doName);
        msg.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                loadin.cancel();
                ToastUtil.showLongToast(FunsDomainNameSelectActivity.this,"出错了");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                loadin.cancel();
                String res = response.body().string();
                String aa=res;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String  str="";

                        if(aa.indexOf("被屏蔽的域名")>-1){
                            str="被屏蔽的域名";
                        }else if(aa.indexOf("未查询任何数据")>-1){
                            str="未查询任何数据";
                        }else if(aa.indexOf("当前域名未备案或者备案取消")>-1){
                            str="当前域名未备案或者备案取消";
                        }else if(aa.indexOf("当前域名未备案或者备案取消")>-1){
                            str="当前域名未备案或者备案取消";
                        }else if(aa.indexOf("<table class=\"table mb0\">")>-1){
                            str=StringUtil.getMiddleStr(aa,"<table class=\"table mb0\">","</tbody>");
                            List<String> middleStrList = StringUtil.getMiddleStrList(str, "<td class=\"z-tl\">", "<");
                            str="";
                            if(middleStrList.size()>6){
                                str=str+"主办单位:"+middleStrList.get(0)+"\n";
                                str=str+"单位性质:"+middleStrList.get(1)+"\n";
                                str=str+"备案号:"+middleStrList.get(2)+"\n";
                                str=str+"网站名称:"+middleStrList.get(3)+"\n";
                                str=str+"网站首页:"+middleStrList.get(4)+"\n";
                                str=str+"审核时间:"+middleStrList.get(5)+"\n";
                                str=str+"最近检测:"+middleStrList.get(6)+"\n";
                            }else {
                                str="解析错误";
                            }
                        }else {
                            str="未查找任何信息";
                        }

                        resEd.setText(str);
                    }
                });
            }
        });
    }


    public void getBeiAnInfo(View view){
       String domainName=domainNameEd.getText().toString();
            if(domainName.length()<1){
                ToastUtil.showLongToast(this,"请输入域名");
                return;
            }
            domainName(domainName);

    }


}
